<template>
  <div>
    <el-upload list-type="picture-card"
               action="/res/album/swf_upload"
               :headers="header"
               :data="uploadData"
               :before-upload="beforeAvatarUpload"
               :on-preview="handlePictureCardPreview"
               :on-remove="handleRemove"
               :on-success="onSuccess"
               :on-error="onError"
               :file-list="fileList"
               :accept="aceeptExt"
               :auto-upload="true">

      <i slot="default"
         class="el-icon-plus"></i>
      <!--            <div slot="file" slot-scope="{file}">-->
      <!--                <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" >-->
      <!--                <span class="el-upload-list__item-actions">-->
      <!--                    <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)" >-->
      <!--                        <i class="el-icon-zoom-in"></i>-->
      <!--                    </span>-->
      <!--                    <span v-if="!disabled" class="el-upload-list__item-delete"-->
      <!--                          @click="handleDownload(file)" >-->
      <!--                        <i class="el-icon-download"></i>-->
      <!--                    </span>-->
      <!--                    <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)" >-->
      <!--                        <i class="el-icon-delete"></i>-->
      <!--                    </span>-->
      <!--                </span>-->
      <!--            </div>-->
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%"
           :src="dialogImageUrl"
           alt="">
    </el-dialog>

  </div>
</template>

<script>
import {
  getToken
} from "@/util/store";
import {getUpload} from "@/api/base/sysSet/uploadSet";
import constants from "@/const/constants";

export default {
  props: {
    albumId: {
      type: String,
      default: ""
    },
    albumCode: {
      type: String,
      default: ""
    },
    resType: {
      type: String,
      default: constants.FILE_TYPE_PIC
    },
    url:[],
  },
  computed: {
    uploadData () {
      return {
        album_id: this.albumId,
        album_code: this.albumCode,
      }
    }
  },
  watch: {
    resType (newValue) {
		console.log("00000")
		getUpload().then(res=>{
			if(res.data.code==200){
				let data= res.data.data
				this.constant = data
				this.limitFileSize = data.imageFileSize;
				this.aceeptExt = data.imageSuffix;
			}
		})
		if (constants.FILE_TYPE_PIC === newValue) {
			this.limitFileSize = this.constant.imageFileSize;
			this.aceeptExt = this.constant.imageSuffix;
		} else if (constants.FILE_TYPE_VIDEO === newValue) {
			this.limitFileSize = this.constant.videoFileSize;
			this.aceeptExt = this.constant.videoSuffix;
		} else if (constants.FILE_TYPE_FILE === newValue) {
			this.limitFileSize = this.constant.fileFileSize;
			this.aceeptExt = this.constant.fileSuffix;
		}
    }
  },
  data () {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      header: {
        Authorization: getToken()
      },
      limitFileSize: null,
      aceeptExt:null,
      uploadFileUrl: [],
      fileList: [],
	  constant:{}
    }
  },
  created() {
  	getUpload().then(res=>{
  		if(res.data.code==200){
  			let data= res.data.data
  			this.constant = data
			this.limitFileSize = data.imageFileSize;
			this.aceeptExt = data.imageSuffix;
  		}
  	})
  },
  methods: {
    closeDialog () {
      //关闭弹窗回调
      this.$emit('callback', this.uploadFileUrl);
    },
    handleRemove (file) {

    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload (file) {

    },
    beforeAvatarUpload (file) {
      if (file.size > this.limitFileSize * 1024 * 1024) {
        this.$message.error('上传头像图片大小不能超过 ' + this.limitFileSize + 'MB!');
        return false;
      }
      return true;
    },
    onSuccess (response, file, fileList) {
      if (response.code == 200) {
        this.$message.success("上传成功！");
        this.uploadFileUrl.push(response.data);
      } else {
        this.fileList = fileList.filter((item) => {
          if (!(file.name == item.name && file.size == item.size)) {
            return item;
          }
        });
        this.$message.error(response.message);
      }

    },
    onError (err) {
      this.$message.error(err);
    }
  }
}
</script>
<style>
</style>
